package gui.customizer.panes;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import gui.customizer.panes.dhcp.Clients;
import gui.customizer.panes.dhcp.SaveAndSend;
import gui.customizer.panes.dhcp.Subnets;

import javax.swing.JTabbedPane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.SSHConnection;
import net.SSHConnectionProxy;

import core.models.DhcpConfiguration;

public class Dhcp extends JTabbedPane {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5743685412874208062L;
	private Logger logger;
	
	public Dhcp() {
		logger = LoggerFactory.getLogger(Dhcp.class);
		final DhcpConfiguration configuration = new DhcpConfiguration();
		final SSHConnection conn = new SSHConnectionProxy();
		File remoteConf;
		SaveAndSend saveAndSendSubnets = new SaveAndSend();
		SaveAndSend saveAndSendClients = new SaveAndSend();
		ActionListener sendBtn = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					File result = configuration.generateDhcpdConf();
					conn.sendDhcpdConf(result);
				} catch (IOException e1) {
					logger.error("Couldn't fetch dhcpd.conf");
					e1.printStackTrace();
					return;
				}
			}
		};
		saveAndSendSubnets.addListenerToSendButton(sendBtn);
		saveAndSendClients.addListenerToSendButton(sendBtn);
		try {
			remoteConf = conn.downloadDhcpConf();
			configuration.loadConfiguration(remoteConf);
			Subnets subnetsTab = new Subnets(configuration, saveAndSendSubnets);
			Clients clientsTab = new Clients(configuration, saveAndSendClients);
			add(subnetsTab, "DHCP Subnets");
			add(clientsTab, "DHCP Clients");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error("Can't download /etc/dhcp/dhcpd.conf file.");
			e.printStackTrace();
			return;
		}
	}
	
}
